package com.facebook.drawee.view;

import android.graphics.drawable.Drawable;

import com.facebook.common.internal.Preconditions;
import com.facebook.common.logging.FLog;
import com.facebook.drawee.components.DraweeEventTracker;
import com.facebook.drawee.components.DraweeEventTracker.Event;
import com.facebook.drawee.drawable.VisibilityAwareDrawable;
import com.facebook.drawee.drawable.VisibilityCallback;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.interfaces.DraweeHierarchy;

import javax.annotation.Nullable;

public final class DraweeHolder<DH extends DraweeHierarchy> implements VisibilityCallback {
    DH mHierarchy;
    public DraweeController mController;
    private boolean mIsControllerAttached;
    private boolean mTrimmed;
    private boolean mIsHolderAttached;
    private boolean mIsVisible;
    private final DraweeEventTracker mEventTracker;

    public DraweeHolder() {
        this.mIsControllerAttached = false;
        this.mTrimmed = false;
        this.mIsHolderAttached = true;
        this.mIsVisible = true;
        this.mController = null;
        this.mEventTracker = new DraweeEventTracker();
    }

    public final void setController(@Nullable DraweeController draweeController) {
        boolean z = this.mIsControllerAttached;
        if (z) {
            this.detachController();
        }
        if (this.mController != null) {
            this.mEventTracker.recordEvent(Event.ON_CLEAR_OLD_CONTROLLER);
            this.mController.setHierarchy((DraweeHierarchy) null);
        }

        this.mController = draweeController;
        if (this.mController != null) {
            this.mEventTracker.recordEvent(Event.ON_SET_CONTROLLER);
            this.mController.setHierarchy(this.mHierarchy);
        } else {
            this.mEventTracker.recordEvent(Event.ON_CLEAR_CONTROLLER);
        }

        if (z) {
            this.attachController();
        }
    }

    public final void setHierarchy(DH dh) {
        this.mEventTracker.recordEvent(Event.ON_SET_HIERARCHY);
        this.setVisibilityCallback((VisibilityCallback) null);
        this.mHierarchy = Preconditions.checkNotNull(dh);
        this.onVisibilityChange(this.mHierarchy.getTopLevelDrawable().isVisible());
        this.setVisibilityCallback(((VisibilityCallback) this));
        if (this.mController != null) {
            this.mController.setHierarchy(dh);
        }
    }

    private void setVisibilityCallback(@Nullable VisibilityCallback visibilityCallback) {
        Drawable topLevelDrawable = this.getTopLevelDrawable();
        if ((topLevelDrawable instanceof VisibilityAwareDrawable)) {
            ((VisibilityAwareDrawable) topLevelDrawable).a(visibilityCallback);
        }
    }

    public final void onDraw() {
        if (!this.mIsControllerAttached) {
            FLog.wtf(DraweeEventTracker.class, "%x: Draw requested for a non-attached controller %x. %s", new Object[]{Integer.valueOf(System.identityHashCode(this)), Integer.valueOf(System.identityHashCode(this.mController)), this.toString()});
            this.mTrimmed = true;
            this.mIsHolderAttached = true;
            this.mIsVisible = true;
            this.attachOrDetachController();
        }
    }

    public final void onVisibilityChange(boolean z) {
        if (this.mIsHolderAttached != z) {
            mEventTracker.recordEvent(z ? Event.ON_DRAWABLE_SHOW : Event.ON_DRAWABLE_HIDE);
            this.mIsHolderAttached = z;
            this.attachOrDetachController();
        }
    }

    public final void trim() {
        this.mEventTracker.recordEvent(Event.ON_HOLDER_ATTACH);
        this.mTrimmed = true;
        this.attachOrDetachController();
    }

    public final void untrim() {
        this.mEventTracker.recordEvent(Event.ON_HOLDER_DETACH);
        this.mTrimmed = false;
        this.attachOrDetachController();
    }

    public final Drawable getTopLevelDrawable() {
        return this.mHierarchy == null ? null : this.mHierarchy.getTopLevelDrawable();
    }

    private void attachController() {
        if (!this.mIsControllerAttached) {
            this.mEventTracker.recordEvent(Event.ON_ATTACH_CONTROLLER);
            this.mIsControllerAttached = true;
            if (this.mController != null && this.mController.getHierarchy() != null) {
                this.mController.onAttach();
            }
        }
    }

    private void detachController() {
        if (this.mIsControllerAttached) {
            this.mEventTracker.recordEvent(Event.ON_DETACH_CONTROLLER);
            this.mIsControllerAttached = false;
            if (this.mController != null) {
                this.mController.onDetach();
            }
        }
    }

    private void attachOrDetachController() {
        if (!this.mTrimmed || !this.mIsHolderAttached || !this.mIsVisible) {
            this.detachController();
        } else {
            this.attachController();
        }
    }

    public final String toString() {
        return android.support.design.a.toStringHelper(this).add("controllerAttached", this.mIsControllerAttached).add("holderAttached", this.mTrimmed).add("drawableVisible", this.mIsHolderAttached).add("activityStarted", this.mIsVisible).add("events", this.mEventTracker.toString()).toString();
    }
}

